//
//  LRXtipAlerView.m
//  路人侠
//
//  Created by zhangxulong on 5/10/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXtipAlerView.h"
@interface LRXTipAlerView()
@property (nonatomic,strong) UIView * content;
@property (nonatomic,strong) UIView * contentSelectMoney;
@property (nonatomic,strong) NSMutableArray * tipBtns;
@end
@implementation LRXTipAlerView
- (instancetype)initTipAlerView
{
    if (self = [super init]) {
        
        CGFloat minX = 54 * wScale;
        CGFloat viewW = MainScreenW - 2 * minX;
        CGFloat imageH = 390 * hScale;
        CGFloat viewH = 470 * hScale;
        CGFloat viewY = 350 * hScale;
        
        UIView * content = [[UIView alloc]initWithFrame:CGRectMake(minX,viewY ,viewW, viewH)];
        UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tipAlerImage"]];
        imageView.frame = CGRectMake(0, 0, viewW, imageH);
        
        
        UIButton * okBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        okBtn.frame = CGRectMake(0, imageH, viewW * 0.5, viewH - imageH);
        okBtn.titleLabel.font = [UIFont systemFontOfSize:30 * hScale];
        okBtn.backgroundColor = [UIColor whiteColor];
        okBtn.tag = 1000;
        [okBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [okBtn setTitle:@"不加" forState:UIControlStateNormal];
        
        
        UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        cancelBtn.frame = CGRectMake(viewW * 0.5, imageH, viewW * 0.5, viewH - imageH);
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:30 * hScale];
        cancelBtn.backgroundColor = [UIColor whiteColor];
        [cancelBtn setTitle:@"好的" forState:UIControlStateNormal];
        cancelBtn.tag = 1001;
        [cancelBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [content addSubview:okBtn];
        [content addSubview:cancelBtn];
        [content addSubview:imageView];
    
        [content addSubview:okBtn];
        [content addSubview:cancelBtn];
        [content addSubview:imageView];
        
        self.content = content;
        
        CGFloat contentImageH = 300 * hScale;
        CGFloat contentSelectH = 560 * hScale;
        
        UIView * contentSelectMoney = [[UIView alloc]initWithFrame:CGRectMake(minX,viewY ,viewW, contentSelectH)];
        contentSelectMoney.backgroundColor = [UIColor whiteColor];
        UIImageView * imageViewSelectMoney = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tipMoneyImage"]];
        imageViewSelectMoney.frame = CGRectMake(0, -10 * hScale, viewW, contentImageH);
        CGFloat btnMinX = 40 * wScale;
        CGFloat btnMinY = CGRectGetMaxY(imageViewSelectMoney.frame) + 27 * hScale;
        CGFloat interW = 30 * wScale;
        CGFloat btnW = (contentSelectMoney.frame.size.width - 2 * (btnMinX + interW)) / 3;
        CGFloat btnH = 60 * hScale;
        NSMutableArray * tipBtns = [NSMutableArray array];
        for(int i = 0; i < 6; i++)
        {
            NSString * title;
            UIButton * money = [UIButton buttonWithType:UIButtonTypeCustom];
            if (i <= 2)
            {
                title = [NSString stringWithFormat:@"%d元",i+1];
                money.tag = 9910 + i + 1;
            }
            
            else
            {
                title = [NSString stringWithFormat:@"%d元",(i-2)*5];
                money.tag = 9910 + (i-2)*5;
            }
            money.titleLabel.font = [UIFont systemFontOfSize:25 * hScale];
            [money setTitle:title forState:UIControlStateNormal];
            [money setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [money setTitleColor:mainColor forState:UIControlStateSelected];
            money.frame = CGRectMake(btnMinX + (i % 3) * (btnW + interW),btnMinY + (btnH + btnMinX) * (i / 3) , btnW, btnH);
            [money addTarget:self action:@selector(tipMoneyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            money.layer.cornerRadius = 5 * hScale;
            money.layer.borderColor = [UIColor grayColor].CGColor;
            money.layer.borderWidth = 2 * hScale;
            [contentSelectMoney addSubview:money];
            [tipBtns addObject:money];
        }
        self.tipBtns = tipBtns;
        
        CGFloat selectbtnY = CGRectGetMaxY(imageViewSelectMoney.frame) + 200 *hScale;
        UIButton * okBtnSelectMoney = [UIButton buttonWithType:UIButtonTypeSystem];
        okBtnSelectMoney.frame = CGRectMake(0, selectbtnY, viewW * 0.5, contentSelectH - selectbtnY);
        okBtnSelectMoney.titleLabel.font = [UIFont systemFontOfSize:30 * hScale];
        okBtnSelectMoney.backgroundColor = [UIColor whiteColor];
        okBtnSelectMoney.tag = 1002;
        [okBtnSelectMoney addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [okBtnSelectMoney setTitle:@"算了" forState:UIControlStateNormal];
        
        
        UIButton * cancelBtnSelectMoney = [UIButton buttonWithType:UIButtonTypeSystem];
        cancelBtnSelectMoney.frame = CGRectMake(viewW * 0.5, selectbtnY, viewW * 0.5, contentSelectH - selectbtnY);
        cancelBtnSelectMoney.titleLabel.font = [UIFont systemFontOfSize:30 * hScale];
        cancelBtnSelectMoney.backgroundColor = [UIColor whiteColor];
        [cancelBtnSelectMoney setTitle:@"加价" forState:UIControlStateNormal];
        cancelBtnSelectMoney.tag = 1003;
        [cancelBtnSelectMoney addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
       
        [contentSelectMoney addSubview:okBtnSelectMoney];
        [contentSelectMoney addSubview:cancelBtnSelectMoney];
        [contentSelectMoney addSubview:imageViewSelectMoney];
        self.contentSelectMoney = contentSelectMoney;
        
        [self addSubview:content];
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.54];
        self.frame = CGRectMake(0, 0, MainScreenW, MainScreenH);
    }
    return self;
}
/**
 *  加小费的各个操作
 *
 *  @param btn tag 1000 表示 不叫
 *                 1001 表示 好的
 *                 1002 表示 算了
 *                 1003 表示 加价
 */

- (void)BtnClick:(UIButton*)btn
{
    CATransition * transition = [[CATransition alloc]init];
    transition.type = @"pageUnCurl";
    [self.layer addAnimation:transition forKey:nil];

    if (btn.tag == 1000) {
        [self.contentSelectMoney removeFromSuperview];
        [self addSubview:self.content];
    }
    else if(btn.tag == 1001)
    {
        [self.content removeFromSuperview];
        [self addSubview:self.contentSelectMoney];
    }
    else if(btn.tag == 1002)
    {
//        [self removeFromSuperview];
        [self.contentSelectMoney removeFromSuperview];
        [self addSubview:self.content];
    }
    else if(btn.tag == 1003)
    {
         [self removeFromSuperview];
    }
}
- (void)tipMoneyBtnClick:(UIButton *)tipBtn
{
    tipBtn.selected = !tipBtn.selected;
    self.tipMoney = [NSString stringWithFormat:@"%ld",tipBtn.tag - 9910];
    
    for (UIButton * btn in self.tipBtns) {
        if (btn.tag == tipBtn.tag) {
            [UIView animateWithDuration:0.5 animations:^{
                btn.titleLabel.font = [UIFont systemFontOfSize:35 * hScale];
            }];
            btn.layer.borderColor = [UIColor colorWithRed:71 green:137 blue:241].CGColor;
        }
        else{
            [UIView animateWithDuration:0.5 animations:^{
                btn.titleLabel.font = [UIFont systemFontOfSize:25 * hScale];
            }];
            btn.selected = NO;
            btn.layer.borderColor = [UIColor grayColor].CGColor;
        }
    }
}
+ (instancetype)tipAlerView
{
    return [[LRXTipAlerView alloc]initTipAlerView];
}

@end
